﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using Puzzler.Common.Exceptions;
using Puzzler.Plugin.Utils;
using NET.Tools;

namespace Puzzler.GUI
{
    /// <summary>
    /// Interaktionslogik für WizardGridPage.xaml
    /// </summary>
    public partial class WizardGridPage : UserControl
    {
        private const int MULTIPLY = 10;

        private IList<Rect> data; 

        public WizardGridPage(IList<Rect> data)
        {
            InitializeComponent();
            this.data = data;
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            Rect? rect = RectangleInputWindow.ShowDialog(null);
            if (rect.HasValue)
            {
                lstGridBox.Items.Add(rect);
                UpdatePreview();
                UpdateData();
            }
        }

        private void UpdatePreview()
        {
            canPreview.Children.Clear();

            foreach (var item in lstGridBox.Items)
            {
                Rect rect = (Rect)item;
                for (int y = (int) rect.Top; y <= rect.Bottom; y++ )
                {
                    for (int x = (int) rect.Left; x <= rect.Right; x++)
                    {
                        canPreview.AddRectangle(
                            x*MULTIPLY, y*MULTIPLY, MULTIPLY, MULTIPLY, 
                            new ShapeStyle(new StrokeStyle(Brushes.Black), new FillStyle(Brushes.White)));
                    }
                }
            }
        }

        private void UpdateData()
        {
            StringBuilder sb = new StringBuilder();
            data.Clear();

            foreach (var item in lstGridBox.Items)
            {
                Rect rect = (Rect)item;
                data.Add(rect);
            }
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Text (*.txt)|*.txt";
            dlg.DefaultExt = "txt";

            if (dlg.ShowDialog().GetValueOrDefault(false))
            {
                try
                {
                    using (StreamWriter writer = new StreamWriter(dlg.FileName, false, Encoding.UTF8))
                    {
                        foreach (Rect rect in lstGridBox.Items)
                        {
                            writer.WriteLine(rect.ToString().Replace(';',','));
                        }
                    }
                }
                catch (IOException ex)
                {
                    MessageBox.Show("Cannot save file! (" + ex.Message + ")", "Error", MessageBoxButton.OK,
                                    MessageBoxImage.Error);
                }
            }
        }

        private void btnLoad_Click(object sender, RoutedEventArgs e)
        {
            if (lstGridBox.Items.Count > 0)
            {
                if (MessageBox.Show("All items will be cleared first! You are soure?", "Load from file", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.No)
                {
                    return;
                }
            }

            lstGridBox.Items.Clear();

            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Text (*.txt)|*.txt";
            dlg.DefaultExt = "txt";

            if (dlg.ShowDialog().GetValueOrDefault(false))
            {
                try
                {
                    using (StreamReader streamReader = new StreamReader(dlg.FileName, Encoding.UTF8))
                    {
                        String line = null;
                        while ((line = streamReader.ReadLine()) != null)
                        {
                            try
                            {
                                lstGridBox.Items.Add(Rect.Parse(line));
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show("Cannot read file: Wrong rect format! (" + ex.Message + ")", "Error",
                                                MessageBoxButton.OK, MessageBoxImage.Error);
                                return;
                            }
                        }
                    }
                }
                catch (IOException ex)
                {
                    MessageBox.Show("Cannot load file! (" + ex.Message + ")", "Error", MessageBoxButton.OK,
                                    MessageBoxImage.Error);
                    lstGridBox.Items.Clear();
                    return;
                }
            }

            UpdatePreview();
            UpdateData();
        }
    }
}
